<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2024/4/10
 * Time: 12:01
 */

namespace plugin\admin\app\logic\queue;


use plugin\admin\app\logic\BaseLogic;
use plugin\admin\app\model\font\FontModel;
use plugin\admin\app\model\queue\AiImageRequestQueueModel;
use plugin\admin\app\model\queue\RedisQueueModel;
use support\Request;
use support\Response;

class RedisQueueLogic extends BaseLogic
{
    protected static $instance;

    public function __construct()
    {
        $this->model = new RedisQueueModel();
    }

    public static function getInstance()
    {
        //        if(is_object(self::$instance)){
//            return self::$instance;
//        }

        return new self();
    }

    public function select(Request $request): Response
    {
        $field = $request->input('field');
        if($field==false){
            $request->inputSet('field','created_at');
            $request->inputSet('order','desc');
        }
        if($request->input('status')==3){
            $request->inputSet('exec_time',['not null']);
            $request->inputSet('finish_at',['null']);
            $request->inputSet('status',0);
        }else if($request->input('status')==='0'){
            $request->inputSet('exec_time',['null']);
            $request->inputSet('finish_at',['null']);
            $request->inputSet('status',0);
        }
        return parent::select($request);
    }

    protected function formatNormal($items, $total): Response
    {
        $nameMap = [
            'requestAiImage'=>'生图队列-客户端',
            'requestAiImageAdmin'=>'生图队列-后台',
            'asyncGeneratedVideoData'=>'生成作品队列',
            'asyncPlanPart'=>'后台AI分段队列',
            'asyncPlanPartAiImage'=>'用户生图队列',
            'asyncVideoPart'=>'二创视频解析队列',
            'batchGeneratePlan'=>'批量生成方案',
            'generateTimbre'=>'生成语音队列',
            'asyncPlanPartImage'=>'图生图队列-后台',
            'refundDealWith'=>'退款处理队列',
            'asyncVideoMaterial'=>'素材视频分析队列',
            'oceanengineAttribution'=>'巨量引擎回传队列',
        ];

        foreach($items as &$val){
            $val['name_txt'] = $nameMap[$val['name']]??'';
            $val['status_txt'] = $nameMap[$val['name']]??'';

           $val['data'] = $val['data'] ? json_encode(json_decode($val['data'], true),JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) : '';
           $val['result'] = $val['result'] ? json_encode(json_decode($val['result'], true),JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) : '';

           if(empty($val['finish_at']) && !empty($val['exec_time'])){
               $val['status'] = 3;
           }
        }

        //$totalData = $this->getWithdrawTotal();
        $totalData = [];

        return json(['code' => 0, 'msg' => 'ok', 'count' => $total, 'data' => $items, 'total_data' => $totalData]);
    }

    public function getWithdrawTotal()
    {
        $data = [];
        [$where, $format, $limit, $field, $order] = $this->selectInput(request());

        if(isset($where['source']) && is_array($where['source'])){
            $where[] = ['source',$where['source'][0],"%{$where['source'][1]}%"];
            unset($where['source']);
        }
        if(isset($where['status']) && is_array($where['status'])){
            $where[] = ['status',$where['status'][0],$where['status'][1]];
            unset($where['status']);
        }
        if(isset($where['tts']) && is_array($where['tts'])){
            $where[] = ['tts','between',$where['tts']];
            unset($where['tts']);
        }
        if(!empty($where['created_at'])){
            $where[] = ['created_at','between',$where['created_at']];
            unset($where['created_at']);
        }

        //统计排队中数
        $where1 = $where;
        $where1[] = ['status','=',0];
        $data['status_0_nums'] = $this->model->getWhere($where1)->count();

        //统计失败数
        $where1 = $where;
        $where1[] = ['status','in',[3,4]];
        $data['status_3_nums'] = $this->model->getWhere($where1)->count();

        //统计完成数
        $where1 = $where;
        $where1[] = ['status','=',2];
        $data['status_2_nums'] = $this->model->getWhere($where1)->count();

        //超过30秒数
        $where1 = $where;
        $where1[] = ['tts','>',30];
        $data['greater_than_30_seconds_nums'] = $this->model->getWhere($where1)->count();

        //近10分钟失败数
        $where1 = [];
        $where1[] = ['status','in',[3,4]];
        $where1[] = ['start_time','>=',date('Y-m-d H:i:s',(time()-1800))];
        $data['nearly_30_minutes_fail_nums'] = $this->model->getWhere($where1)->count();

        return $data;
    }
}